package com.yitiao.data_algorithms.leetcode.editor.cn;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

class OddEvenLinkedList{
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode oddEvenList(ListNode head) {
        ListNode ji = new ListNode(0);
        ListNode ou = new ListNode(0);
        ListNode ouHead = ou;
        ListNode jiHead = ji;

        int flag = 1;
        while (head!=null){
            ListNode temp = head;
            head = head.next;
            if (flag==1){
                ji.next = temp;
                ji = ji.next;
                flag = 0;
            }else {
                ou.next = temp;
                ou = ou.next;
                flag = 1;
            }
            temp.next = null;
        }
        ji.next = ouHead.next;
        return jiHead.next;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}